package com.znf4.controller.good;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.znf4.constants.CommonConst;
import com.znf4.enums.OperationStatusEnum;
import com.znf4.good.imgUrl.entity.ImgUrl;
import com.znf4.good.imgUrl.server.ImgUrlServer;
import com.znf4.id.IdGenerator;
import com.znf4.utils.OSSFile;

/**
 * 商品多张图片
 */
@Controller
@RequestMapping("/good/imgUrl")
public class ImgUrlController {
	
	
	private static final Logger LOG = LoggerFactory.getLogger(ImgUrlController.class);
	
	@Autowired
	private ImgUrlServer imgUrlServer ;
	
	@Autowired
	private IdGenerator idGenerator ;
	
   
	
	/**
	 * 函数功能说明 ： 添加商品图片
	 *
	 * @参数： @return @return String @throws
	 * @param imgUrl 图片基本信息
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping(value = "/add", method = { RequestMethod.POST, RequestMethod.GET })
	public String add(ImgUrl imgUrl, String type, Model model, HttpServletRequest request) {

		try {
			imgUrl.setId(idGenerator.getGoodNameNumber());
			imgUrlServer.addGoodImgUrl(imgUrl);
			long startTime = System.currentTimeMillis();

			String fileName;
			if (type != null && type.equals("crops")) { // 农作物
				fileName = CommonConst.CROPS_GOOD_IMG_NUMBER + imgUrl.getId();
			} else if (type != null && type.equals("free")) { // 免费商品
				fileName = CommonConst.FREE_GOOD_IMG_NUMBER + imgUrl.getId();
			} else if(type != null && type.equals("jifen")){ // 积分商品
				fileName = CommonConst.JIFEN_GOOD_IMG_NUMBER + imgUrl.getId();
			}else{
				model.addAttribute("status", OperationStatusEnum.FAILED.getDesc());
				return "good/list" ;
			}

			// 文件上传OSS
			Iterator iter = OSSFile.getFileIterator(request) ;
			if (iter != null ) {
				// 将request变成多部分request
				MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
				// 获取multiRequest 中所有的文件名
				while (iter.hasNext()) {
					// 一次遍历所有文件
					MultipartFile file = multiRequest.getFile(iter.next().toString());					
					imgUrl.setImg_url(OSSFile.upFile(file , fileName));
				}
			}

			model.addAttribute("status", OperationStatusEnum.SUCCESS.getDesc());

			long endTime = System.currentTimeMillis();
			System.out.println("运行时间：" + String.valueOf(endTime - startTime) + "ms");
		} catch (Exception e) {
			LOG.info("添加其他图片 :" , e.getMessage());
			e.printStackTrace();
			model.addAttribute("status", OperationStatusEnum.FAILED.getDesc());
		}
		
		return "good/list";
	}

	
	
	/**
	 * 函数功能说明 ： 修改商品图片
	 *
	 * @参数： @return
	 * @return String
	 * @throws
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping(value = "/update", method = { RequestMethod.POST, RequestMethod.GET })
	public String update(ImgUrl imgUrl , String type , Model model , HttpServletRequest request ) {
		try {
			
			String fileName;
			if (type != null && type.equals("crops")) { // 农作物
				fileName = CommonConst.CROPS_GOOD_IMG_NUMBER + imgUrl.getId();
			} else if (type != null && type.equals("free")) { // 免费商品
				fileName = CommonConst.FREE_GOOD_IMG_NUMBER + imgUrl.getId();
			} else if (type != null && type.equals("jifen")) { // 积分商品
				fileName = CommonConst.JIFEN_GOOD_IMG_NUMBER + imgUrl.getId();
			} else {
				model.addAttribute("status", OperationStatusEnum.FAILED.getDesc());
				return "good/list";
			}
			// 文件上传OSS
			Iterator iter = OSSFile.getFileIterator(request);
			if (iter != null) {
				// 将request变成多部分request
				MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
				// 获取multiRequest 中所有的文件名
				while (iter.hasNext()) {
					// 一次遍历所有文件
					MultipartFile file = multiRequest.getFile(iter.next().toString());
					imgUrl.setImg_url(OSSFile.upFile(file , fileName));
				}
			}
			imgUrlServer.updateGoodImgUrl(imgUrl);
			model.addAttribute("status", OperationStatusEnum.SUCCESS.getDesc());

		} catch (Exception e) {
			LOG.info("修改其他图片 :" , e.getMessage());
			e.printStackTrace();
			model.addAttribute("status", OperationStatusEnum.FAILED.getDesc());
		}
		return "good/list";
	}

	/**
	 * 函数功能说明 ： 删除农作物商品
	 *
	 * @参数： @return
	 * @return String
	 * @throws
	 */
	@RequestMapping(value = "/delOne", method = { RequestMethod.POST, RequestMethod.GET })
	public String delOne(ImgUrl imgUrl , Model model , HttpServletRequest request ) {
		
		try {

			imgUrlServer.delOneGoodImgUrl(imgUrl);
			// 删除文件 OSS
			OSSFile.deleteFile(imgUrl.getImg_url());
			model.addAttribute("status", OperationStatusEnum.SUCCESS.getDesc());

		} catch (Exception e) {
			LOG.info("删除单个其他图片 :", e.getMessage());
			e.printStackTrace();
			model.addAttribute("status", OperationStatusEnum.FAILED.getDesc());
		}
		
		return "good/list";
	}

	/**
	 * 函数功能说明 ： 删除多条农作物商品
	 *
	 * @参数： @return
	 * @return String
	 * @throws
	 */
	@RequestMapping(value = "/delAll", method = { RequestMethod.POST, RequestMethod.GET })
	public String delAll(String[] imgUrls, Model model) {

		try {
			List<ImgUrl> list = new ArrayList<>();
			for (int i = 0; i < imgUrls.length; i++) {

				ImgUrl imgUrl = new ImgUrl();
				imgUrl.setId(imgUrls[i]);
				list.add(imgUrl);

				// 删除文件
				ImgUrl delImg = imgUrlServer.queryOne(imgUrl);
				OSSFile.deleteFile(delImg.getImg_url());

			}
			imgUrlServer.delAllGoodImgUrl(list);
			model.addAttribute("status", OperationStatusEnum.SUCCESS.getDesc());

		} catch (Exception e) {
			LOG.info("删除多个其他商品图片 ：" , e.getMessage() );
			e.printStackTrace();
			model.addAttribute("status", OperationStatusEnum.FAILED.getDesc());
		}
		return "good/list";
	}
	
	
	/**
	 * 函数功能说明 ： 获取商品所有的图片
	 *
	 * @参数： @return
	 * @return String
	 * @throws
	 */
	@RequestMapping(value = "/queryAll", method = { RequestMethod.POST, RequestMethod.GET })
	public String queryAll(ImgUrl imgUrl , Model model) {
		
		List<ImgUrl> list = imgUrlServer.queryAll(imgUrl);
		model.addAttribute("status", OperationStatusEnum.SUCCESS.getDesc() );
		model.addAttribute("data", list );
		return "good/list";
	}
	

	

}
